package com.yjy.service;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yjy.entity.Account;
import com.yjy.entity.Admin;
import com.yjy.exception.CustomerException;
import com.yjy.mapper.AdminMapper;
import com.yjy.utils.TokenUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminService {
    @Resource
    AdminMapper adminMapper;

    public Admin selectById(String userId) {
        return adminMapper.selectById(userId);
    }

    public String admin(String name) {
        if("admin".equals(name)){
            return "admin";
        }else{
//            return "账号错误";//自定义异常
            throw new CustomerException("账号错误!");
        }
    }

    public List<Admin> selectAll(Admin admin){
        return adminMapper.selectAll(admin);
    }

    public PageInfo<Admin> selectPage(Integer pageNum, Integer pageSize,Admin admin) {
        //开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        List<Admin> admins = adminMapper.selectAll(admin);
        return PageInfo.of(admins);
    }

    public void add(Admin admin) {
        // 根据新的账号查询数据库 是否存在同样账号的数据
        Admin dbAdmin = adminMapper.selectByUsername(admin.getUsername());
        if(dbAdmin!=null){
            throw new CustomerException("账号重复");

        }
//        if(admin.getPassword() == null || "".equals(admin.getPassword())){
//
//        }
        //用hutool
        //默认密码
        if(StrUtil.isBlank(admin.getPassword())) {
            admin.setPassword("admin");
        }
        admin.setRole("ADMIN");
        adminMapper.insert(admin);
    }

    public void update(Admin admin) {
        adminMapper.updateById(admin);
    }

    public void deleteById(Integer id) {
        adminMapper.deleteById(id);
    }

    public void deleteBatch(List<Admin> list) {
//        for (Admin admin : list) { //iter快捷键
//            deleteById(admin.getId());
//        }  效率太低

        adminMapper.deleteBatch(list);
    }

    public Admin login(Account admin) {
        Admin dbAdmin = adminMapper.selectByUsername(admin.getUsername());
        if(dbAdmin == null){
            throw new CustomerException("账号不存在");
        }
        // 验证账号密码是否正确
        if(!dbAdmin.getPassword().equals(admin.getPassword())){
            throw  new CustomerException("账号或密码错误");
        }
        //创建token并返回给前端
        String token = TokenUtils.createToken(dbAdmin.getId() + "-" + "ADMIN", dbAdmin.getPassword());
        dbAdmin.setToken(token);
        return dbAdmin;
    }

    public void updatePassword(Account account) {
        // 先判断新密码和确认密码是否一致
        if(!account.getNewPassword().equals(account.getNew2Password())) {
            throw new CustomerException("500","您两次输入的密码不一致!请重新输入!");
        }
        // 校验一下原密码是否正确
        Account currentUser = TokenUtils.getCurrentUser();
        if(!account.getPassword().equals(currentUser.getPassword())){
            throw new CustomerException("500","原密码输入错误!");
        }

        //开始更新密码
        Admin admin = adminMapper.selectById(String.valueOf(currentUser.getId()));
        admin.setPassword(account.getNewPassword());
        adminMapper.updateById(admin);
    }
}






















